package com.admin.userlogin.controller;


import com.admin.userlogin.pojo.User;
import com.admin.userlogin.service.UserService;
import com.admin.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 移动端用户表 前端控制器
 * </p>
 *
 * @author y-x
 * @since 2022-12-15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("login")//传来data
    public R login(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        User obj = userService.getOne(wrapper);
        return userService.getOne(wrapper)!=null?R.ok().data("user",obj):R.error();
    }
    @GetMapping("info/{token}")
    public R getInfo(@PathVariable Integer token){
        User user = userService.getById(token);
        return userService.getById(token)!=null?R.ok().data("user",user):R.error();
    }

    //注册的
    @PostMapping("register")//传来data
    public R register(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        User user1 = userService.getOne(wrapper);
        if (Objects.isNull(user1)){
            System.out.println(user1);
            userService.save(user);
            return R.ok();
        }
        return R.error().data("msg","账号已存在");
    }
}

